/**
 * Copyright &copy; 2012-2016 < All rights reserved.
 */
package com.jeemicro.weixin.modules.cms.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeemicro.weixin.common.persistence.Page;
import com.jeemicro.weixin.common.utils.StringUtils;
import com.jeemicro.weixin.common.web.BaseController;
import com.jeemicro.weixin.modules.cms.entity.Baoming;
import com.jeemicro.weixin.modules.cms.entity.CmsHuiyuan;
import com.jeemicro.weixin.modules.cms.service.BaomingService;
import com.jeemicro.weixin.modules.cms.service.CmsHuiyuanService;
import com.jeemicro.weixin.modules.sys.utils.DictUtils;
import com.jeemicro.weixin.modules.sys.utils.UserUtils;

/**
 * 留言Controller
 * @author zmrid
 * @version 2016-3-23
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/baoming")
public class BaomingController extends BaseController {

	@Autowired
	private BaomingService baomingService;

	@ModelAttribute
	public Baoming get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return baomingService.get(id);
		}else{
			return new Baoming();
		}
	}
	
	@RequiresPermissions("cms:baoming:view")
	@RequestMapping(value = {"list", ""})
	public String list(Baoming baoming, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Baoming> page = baomingService.findPage(new Page<Baoming>(request, response), baoming); 
        model.addAttribute("page", page);
     
		return "modules/cms/baomingList";
	}

	@RequiresPermissions("cms:baoming:view")
	@RequestMapping(value = "form")
	public String form(Baoming baoming, Model model) {
		model.addAttribute("baoming", baoming);
		return "modules/cms/baomingForm";
	}

	@RequiresPermissions("cms:baoming:edit")
	@RequestMapping(value = "save")
	public String save(Baoming baoming, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baoming)){
			return form(baoming, model);
		}
		if (baoming.getReUser() == null){
			baoming.setReUser(UserUtils.getUser());
			baoming.setReDate(new Date());
		}
		baomingService.save(baoming);
		addMessage(redirectAttributes, DictUtils.getDictLabel(baoming.getDelFlag(), "cms_del_flag", "保存")
				+"留言'" + baoming.getName() + "'成功");
		return "redirect:" + adminPath + "/cms/baoming/?repage&status=2";
	}
	
	@RequiresPermissions("cms:baoming:edit")
	@RequestMapping(value = "delete")
	public String delete(Baoming baoming, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		baomingService.delete(baoming, isRe);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"恢复审核":"删除")+"留言成功");
		return "redirect:" + adminPath + "/cms/baoming/?repage&status=2";
	}

}
